package com.qingyun.common.util;


import com.qingyun.common.dto.UserDTO;
import org.springframework.stereotype.Component;

@Component
public class UserHolder {
    private static final ThreadLocal<UserDTO> CONTEXT = new ThreadLocal<>();

    public void set(UserDTO user) {
        CONTEXT.set(user);
    }

    public UserDTO get() {
        return CONTEXT.get();
    }

    public Long getUserId() {
        UserDTO u = CONTEXT.get();
        return u == null ? null : u.getId();
    }

    public void clear() {
        CONTEXT.remove();
    }
}


